package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.BasicPlatformMapper;
import com.macro.mall.mapper.SpsDataContentMapper;
import com.macro.mall.mapper.SpsDataDiscussMapper;
import com.macro.mall.model.PinionParam;
import com.macro.mall.model.SpsDataContent;
import com.macro.mall.model.SpsDataDiscuss;
import com.macro.mall.pojo.DataManPlatformdVo;
import com.macro.mall.pojo.SpsDataContentVo;
import com.macro.mall.pojo.TopicContentVo;
import com.macro.mall.query.PinionQuery;
import com.macro.mall.service.SysDataManService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SysDataManServiceImpl implements SysDataManService {


    @Autowired
    private SpsDataContentMapper contentMapper;

    @Autowired
    private SpsDataDiscussMapper discussMapper;


    @Autowired
    private BasicPlatformMapper platformMapper;

    /****
     * 获取舆情研判列表
     * @param param
     * @return
     */
    @Override
    public CommonResult getJudgment(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        PinionQuery query = new PinionQuery();
        BeanUtils.copyProperties(param, query);
        List<SpsDataContentVo> contentList =  contentMapper.selectList(query);
        Collections.sort(contentList);
        PageInfo<SpsDataContentVo> pageInfo = new PageInfo<>(contentList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 编辑评论
     * @param content
     * @return
     */
    @Override
    public CommonResult edit(SpsDataContent content) {
        if (content.getId() == null || content.getId() <= 0){
            return CommonResult.failed("新增评论");
        }
        content.setUpdatedDt(new Date());
        int row = contentMapper.updateByPrimaryKeySelective(content);
        return result(row, "编辑失败");
    }


    /***
     * 批量删除作品表
     * @param id
     * @return
     */
    @Override
    public CommonResult delete(String id) {
        String[] split = id.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : split) {
            idList.add(Long.parseLong(s));
        }
        if (!idList.isEmpty()){
            int row = contentMapper.deleteBtchByPrimaryKey(idList);
            return result(row, "删除失败");
        }
        return CommonResult.failed("删除失败");
    }


    /****
     * 获取内容下的评论
     * @param param
     * @return
     */
    @Override
    public CommonResult getDiscuss(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        System.out.println("" + param.getContentId());
        List<SpsDataDiscuss> discussList = discussMapper.selectByContentId(param.getContentId());
        PageInfo<SpsDataDiscuss> pageInfo = new PageInfo<>(discussList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 获取舆情话题
     * @param param
     * @return
     */
    @Override
    public CommonResult getOpinionTopic(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        PinionQuery query = new PinionQuery();
        BeanUtils.copyProperties(param, query);
        //查询舆情话题
        List<TopicContentVo> topicList =  discussMapper.selectByOpinionTopic(query);
        for (TopicContentVo topicContentVo : topicList) {
            topicContentVo.setHeatQuantity(topicContentVo.getPvQuantity() + topicContentVo.getVisitsQuantity() * 10);
        }
        PageInfo<TopicContentVo> pageInfo = new PageInfo<>(topicList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }

    /****
     * 查询舆情话题详情
     * @param param
     * @return
     */
    @Override
    public CommonResult getOpinionItem(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        PinionQuery query = new PinionQuery();
        BeanUtils.copyProperties(param, query);
        //查询舆情话题详情
        List<SpsDataContentVo> contentList =  discussMapper.selectOpinionItem(query);
        PageInfo<SpsDataContentVo> pageInfo = new PageInfo<>(contentList);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 查询数据管理-平台数据
     * @param param
     * @return
     */
    @Override
    public CommonResult getPlatformData(PinionParam param) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(param.getPage(), param.getPageSize());
        PinionQuery query = new PinionQuery();
        BeanUtils.copyProperties(param, query);
        List<DataManPlatformdVo> dataManPlatformdVos = platformMapper.selectByName(query);
        for (DataManPlatformdVo dataManPlatformdVo : dataManPlatformdVos) {
            dataManPlatformdVo.setHeatCount(dataManPlatformdVo.getPvCount() + dataManPlatformdVo.getVisitsCount() * 10);
        }
        PageInfo<DataManPlatformdVo> pageInfo = new PageInfo<>(dataManPlatformdVos);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return CommonResult.success(resultMap);
    }


    /***
     * 判断修改数据库返回值 返回
     * @param row
     * @param msg
     * @return
     */
    private CommonResult result(int row, String msg) {
        if (row > 0) {
            return CommonResult.success(null);
        }
        return CommonResult.failed(msg);
    }
}
